﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Jamaat_Financial_System.Model.BO;
using Jamaat_Financial_System.Model.DAO;

namespace Jamaat_Financial_System
{
    public partial class Window1 : Window
    {
        private Receipt_DAO ReceiptDAO = new Receipt_DAO();
        private Tanzeem_DAO TanzeemDAO = new Tanzeem_DAO();
        private Halqa_DAO HalqaDAO = new Halqa_DAO();
        private Tajneed_DAO TajneedDAO = new Tajneed_DAO();

        public Window1()
        {
            InitializeComponent();

            ReceiptListView.DataContext = this;
            NewTajneedTanzeem.DataContext = this;
            NewTajneedHalqa.DataContext = this;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (NewReceiptNo.Text.Length != 0)
            {
                Receipt_BO ReceiptBO = new Receipt_BO();
                ReceiptBO.BookNo = NewReceiptNo.Text;
                int responseCode = ReceiptDAO.Insert(ReceiptBO);
                if (responseCode == ErrorCodes_BO.SUCCESS)
                    MessageBox.Show("Successfully Inserted");
                else
                    MessageBox.Show("Some Problem While Inserting. Error Code: " + responseCode);
            }
            else
                MessageBox.Show("Please Enter Some Number");
        }

        public ObservableCollection<Model.BO.Base_BO> ReceiptList
        {
            get { return ReceiptDAO.UpdateList(); }
        }

        public ObservableCollection<Model.BO.Base_BO> TanzeemList
        {
            get { return TanzeemDAO.UpdateList(); }
        }

        public ObservableCollection<Model.BO.Base_BO> HalqaList
        {
            get { return HalqaDAO.UpdateList(); }
        }

        private void ListViewItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (ReceiptListView.SelectedItem != null)
            {
                String BookNo = ((Receipt_BO)ReceiptListView.SelectedItem).BookNo;
                if (BookNo != null)
                {
                    Receipt_BO ReceiptBO = new Receipt_BO();
                    ReceiptBO.BookNo = BookNo;
                    int responseCode = ReceiptDAO.Delete(ReceiptBO);
                    if (responseCode == ErrorCodes_BO.SUCCESS)
                    {
                        ReceiptDAO.UpdateList();
                        MessageBox.Show("Successfully Deleted");
                    }
                    else
                        MessageBox.Show("Some Problem While deleting. Error Code: " + responseCode);
                }
            }
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            if (NewTajneedName.Text.Length == 0 || NewTajneedFatherName.Text.Length == 0 || NewTajneedHalqa.SelectedIndex == -1 || NewTajneedTanzeem.SelectedIndex == -1 || (NewTajneedWasiat.IsChecked == true && (NewTajneedWasiatID.Text.Length == 0 || NewTajneedWasiatHisa.Text.Length == 0 )))
            {
                MessageBox.Show("Please Fill All Values");
            }
            else
            {
                Tajneed_BO TajneedBO = new Tajneed_BO();
                TajneedBO.TajId = NewTajneedId.Text;
                TajneedBO.Name = NewTajneedName.Text;
                TajneedBO.FatherName = NewTajneedFatherName.Text;
                TajneedBO.Address = NewTajneedAddress.Text;
                TajneedBO.City = "Lahore";
                TajneedBO.Tell = NewTajneedTelephone.Text;
                TajneedBO.Mobile = NewTajneedMobile.Text;
                TajneedBO.HalqaId = (string)NewTajneedHalqa.SelectedValue;
                TajneedBO.Tanzeem = (string)NewTajneedTanzeem.SelectedValue;
                TajneedBO.Wasiat = (bool)NewTajneedWasiat.IsChecked;
                TajneedBO.WasiatNo = NewTajneedWasiatID.Text.Equals("")?null:NewTajneedWasiatID.Text;
                if (NewTajneedWasiat.IsChecked == true)
                {
                    TajneedBO.WasiatHisa = Int32.Parse(NewTajneedWasiatHisa.Text);
                }

                int responseCode = TajneedDAO.Insert(TajneedBO);
                if (responseCode == ErrorCodes_BO.SUCCESS)
                {
                    MessageBox.Show("Successfully Inserted");
                }
                else
                    MessageBox.Show("Some Problem While inserting. Error Code: " + responseCode);
            }
        }

    }

}
